<?php
/**
 * Created by PhpStorm.
 * User: superxing-cto
 * Date: 17/1/12
 * Time: 14:50
 */

namespace App\Models;

/**
 * Class RedPackageOrder
 * @property string ping_id
 * @property string object
 * @property string created
 * @property string received
 * @property string refunded
 * @property string livemode
 * @property string status
 * @property string app
 * @property string channel
 * @property string order_no
 * @property string transaction_no
 * @property string amount
 * @property string amount_settle
 * @property string currency
 * @property string recipient
 * @property string subject
 * @property string body
 * @property string description
 * @property string failure_msg
 * @property string user_id
 * @property string send_time
 * @property string received_time
 */
class RedPackageOrder extends ApplicationModel
{
    protected $guarded = [];

    public static function saveOrder($id, $order)
    {
        $red_package_order = new self();
        $red_package_order->ping_id = $order['id'];
        $red_package_order->object = $order['object'];
        $red_package_order->created = $order['created'];
        $red_package_order->received = $order['received'];
        $red_package_order->refunded = $order['refunded'];
        $red_package_order->livemode = $order['livemode'];
        $red_package_order->status = $order['status'];
        $red_package_order->app = $order['app'];
        $red_package_order->channel = $order['channel'];
        $red_package_order->order_no = $order['order_no'];
        $red_package_order->transaction_no = $order['transaction_no'];
        $red_package_order->amount = $order['amount'];
        $red_package_order->amount_settle = $order['amount_settle'];
        $red_package_order->currency = $order['currency'];
        $red_package_order->recipient = $order['recipient'];
        $red_package_order->subject = $order['subject'];
        $red_package_order->body = $order['body'];
        $red_package_order->description = $order['description'];
        $red_package_order->failure_msg = $order['failure_msg'];
        $red_package_order->user_id = $id;
        $red_package_order->save();
    }

    public static function updateOrderSent($obj)
    {
        $sj_order = self::where('ping_id', $obj['id'])->orWhere('order_no', $obj['order_no'])->first();
        if ($sj_order) {
            $sj_order->status = $obj['status'];
            $sj_order->transaction_no = $obj['transaction_no'];
            $sj_order->send_time = date('Y-m-d H:i:s');
            $sj_order->save();
        }
    }

    public static function updateOrderReceived($obj)
    {
        $sj_order = self::where('ping_id', $obj['id'])->orWhere('order_no', $obj['order_no'])->first();
        if ($sj_order) {
            $sj_order->status = $obj['status'];
            $sj_order->transaction_no = $obj['transaction_no'];
            $sj_order->received_time = date('Y-m-d H:i:s');
            $sj_order->save();
        }
    }
}